var pack = require('./operation/index.js');
// var arr = process.argv;
// if (arr.length > 3) {
// 	throw new Error("只允许通过cwd传递一个参数");
// }
// var str = arr[arr.length - 1];
const readline = require('readline');
const mappings = require('./config/mappings.js');
const consoleColor = require('./util/console-color.js')
const rl = readline.createInterface({
	input: process.stdin,
	output: process.stdout
});

var question = "请输入方括号中的内容，以确定执行打包程序\r\n";
var keys = Object.keys(mappings);
question += "[clear]:清空窗口信息\r\n";
question += "[all]:执行所有已知程序的打包程序\r\n";
question += "[quit]:退出\r\n";
for (var i = 0; i < keys.length; i++) {
	var k = keys[i];
	question += `[${i}]:${mappings[k].name}\r\n`;
}
async function begin(key) {
	await pack(key);
}
rl.on('close', () => {
	process.exit(0);
});
rl.on('error', () => {
	process.exit(0);
});
// ask user for the anme input
function init() {
	console.log(consoleColor.green, question, consoleColor.white);
	rl.question('', (name) => {
		// ask for nationality
		if (name === 'all') {
			begin('all').then(res => {
				console.log('进程执行结束。请查看运行结果')
				init();
			})
		} else if (name === 'clear') {
			console.clear();
			init();
		}else if (name === 'quit') {
			process.exit(0);
		} else
		if (isNaN(name) || !keys[name]) {
			console.clear();
			console.error('输入错误，请重新输入')
			init();
		} else {
			begin(keys[name]).then(res => {
				console.log('进程执行结束。请查看运行结果')
				init();
			})
		}
	});

}
init();
